﻿using System;
using System.Reflection;

public class PassiveObject : ActiveObject
{

    public string linkScript;

    private int passiveState;
    
    private Type linkScriptType;
    private object linkScriptObj;
    private PassiveObjectType passiveType;

    public int PassiveID;

    public new int getState()
    {
        return passiveState;
    }

    private void Start()
    {

        initAnimator();
        initRighdBody();
        initBoxCollider();
        initGravityEvent();
        initActiveObjectAttackEvent();
        initObjectPool();
        setObjectType(ObjectType.PASSIVE);

        ObjectManager.pushPassiveObject(this);

        if (linkScript != null)
        {
            linkScriptType = Type.GetType("Unity.Game.ElisAdvance.PassiveScript." + linkScript);
            object[] constructParms = new object[] { this };  //构造器参数
            linkScriptObj = Activator.CreateInstance(linkScriptType, constructParms);
            MethodInfo method = linkScriptType.GetMethod("onInit");
            method.Invoke(linkScriptObj, null);
        }

    }

    private void Awake()
    {
        initAnimator();
        initRighdBody();
        initBoxCollider();
    }

    public void setState(int state)
    {
        
        if (linkScriptType != null && linkScriptObj != null)
        {
            MethodInfo method = linkScriptType.GetMethod("onSetState");
            if (method != null)
                method.Invoke(linkScriptObj, new object[] { state });
        }
        passiveState = state;
    }

    public override void sendDestroy()
    {
        ObjectManager.getPassiveList().Remove(this);
        isDestroy = true;
        Destroy(gameObject);
    }

    public override void sendHit(ActiveObject attacker, float damage, DamageBounsType type, params float[] vector)
    {
        if (isDamageable() == false)
            return;

        float hit = DamageBouns.getPassiveLastBouns(this, damage);
        setHp(getHp() - hit);
    }

    void Update()
    {
        onUpdateActiveObjectEvent();

        if (linkScriptType != null && linkScriptObj != null)
        {
            MethodInfo method = linkScriptType.GetMethod("onProc");
            if (method != null)
                method.Invoke(linkScriptObj, null);
        }


    }

    private void FixedUpdate()
    {
        onFixUpdateActiveObjectEvent();

    }

}

